var httpsUrl = getApp().globalData.httpsUrl;
var globalData = getApp().globalData;
var commonUtil = require("../../../utils/commonUtil.js")
var imgUrl = globalData.imgUrl;
Page({
  data: {
    imgUrl: imgUrl,
    list:[],
    pageNo: 1,
    pageSize: 50,
  },
  list: function () {
    var that = this;
    var params = new Object();
    params.pageNo = this.data.pageNo;
    params.pageSize = this.data.pageSize;
    console.log("params:" + JSON.stringify(params));
    let openId = wx.getStorageSync("openidKey");
    commonUtil.showLoading("加载中...");
    wx.request({
      url: httpsUrl + "/memberAddress/getAddress",
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        commonUtil.hideLoading();
        res = res.data;
        console.log("res:" + JSON.stringify(res));
        if (res.result == 0) {
          var rows = res.data.list;
          that.setData({ list: rows })
        } else {
          commonUtil.showModal(res.msg);
        }
      }
    })
  },
  edit:function(e){
    let index = e.currentTarget.id;
    let list = this.data.list;
    wx.setStorageSync("addressInfo", list[index]);
    wx.navigateTo({
      url: '../editAddress/editAddress',
    })
  },
  add: function(e) {
    wx.setStorageSync("addressInfo", new Object());
    wx.navigateTo({
      url: '../editAddress/editAddress',
    })
  },
  delete: function (e) {
    let id = e.currentTarget.id;
    var that = this;
    var params = new Object();
    params.addressId = id;
    console.log("params:" + JSON.stringify(params));
    let openId = wx.getStorageSync("openidKey");
    commonUtil.showLoading("加载中...");
    wx.request({
      url: httpsUrl + "/memberAddress/deleteAddress",
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        commonUtil.hideLoading();
        res = res.data;
        console.log("删除res:" + JSON.stringify(res));
        if (res.result == 0) {
          that.list();
        } else {
          commonUtil.showModal(res.msg);
        }
      }
    })
  },
  moren:function(e){
    let that = this;
    let index = e.currentTarget.id;
    let list = this.data.list;
    let info = list[index];
    info.isDefault = true;
    console.log("参数：" + JSON.stringify(info));
    commonUtil.showLoading("请稍等...");
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, },
      url: httpsUrl + "/memberAddress/editAddress",
      dataType: "json",
      method: "POST",
      data: info,
      success: function (res) {
        commonUtil.hideLoading();
        console.log("res：" + JSON.stringify(res));
        res = res.data;
        if (res.result == 0) {
          that.list();
        } else {
          commonUtil.showModal(res.msg);
        }
      }
    })
  },
  select:function(e){
    let that = this;
    let index = e.currentTarget.id;
    let list = this.data.list;
    let info = list[index];
    let returnAddress = wx.getStorageSync("returnAddress");
    if (returnAddress == 1){
      wx.setStorageSync("addressInfo", info);
      wx.navigateBack({
        delta: 1
      })
      // wx.navigateTo({
      //   url: '../../order/submitOrder/submitOrder',
      // })
    }
  },
  onShow: function (options) {
    wx.removeStorageSync("addressInfo");
    this.list();
  },
  
})